import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ModalController } from 'ionic-angular';
import { ApiProvider } from '../../../providers/api/api';
import { NotificationProvider } from '../../../providers/notification/notification';
import { PAGE_COUNT } from '../../../app/app.constants';

/**
 * Generated class for the ScanListPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-scan-list',
  templateUrl: 'scan-list.html',
})
export class ScanListPage {
  private funid = 'sp_scan_ans';
  private items: Array<string>;
  private searchValue: string;
  private options = {
    start: 0,
    limit: PAGE_COUNT,
    where_sql: '',
    where_type: '',
    where_value: ''
  };
  private searchTimer: any;


  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private api: ApiProvider,
    private notification: NotificationProvider,
    public modalCtrl: ModalController) { }

  ionViewDidLoad() {
    this.notification.showLoading();
    let res = this.api.getListPage(this.funid, this.options).subscribe(res => {
      if (res['success']) {
        this.items = res['data']['root'];
      } else {
        this.notification.showAlert(res['message']);
      }
      this.notification.hideLoading();
    })
  }


  /**
   * 
   * @param store 查看详情
   * @param index 
   */
  showDetail(item) {
    this.navCtrl.push('ScanDetailPage', { 'scan_id': item.sp_scan__scan_id });
  }

  /**
   * 搜索存货
   */
  getItems(e) {
    clearTimeout(this.searchTimer);
    this.searchTimer = setTimeout(() => {
      this.options.where_sql = "sp_store.sp_name like ?";
      this.options.where_type = "string";
      this.options.where_value = '%' + this.searchValue + '%';
      this.doSearch();
    }, 1000);
  }

  /**
   * 执行搜索
   */
  doSearch() {
    this.notification.showLoading();
    this.options.start = 0;
    let res = this.api.getListPage(this.funid, this.options).subscribe(res => {
      if (res['success']) {
        this.items = res['data']['root'];
      } else {
        this.notification.showAlert(res['message']);
      }
      this.notification.hideLoading();
    })
  }

  /**
   * 下拉刷新
   * @param refresher 
   */
  doRefresh(refresher) {
    this.options.start = 0;

    let res = this.api.getListPage(this.funid, this.options).subscribe(res => {
      if (res['success']) {
        this.items = res['data']['root'];
      } else {
        this.notification.showAlert(res['message']);
      }
      refresher.complete();
    })

  }

  /**
   * 上拉加载
   * @param infiniteScroll 
   */
  doInfinite(infiniteScroll) {
    console.log('Begin async operation');
    this.options.start += PAGE_COUNT;
    this.api.getListPage(this.funid, this.options).subscribe(res => {
      if (res['success']) {
        let newItems = res['data']['root'];
        newItems.forEach(item => {
          this.items.push(item);
        });
      } else {
        this.notification.showAlert(res['message']);
      }
      infiniteScroll.complete();
    });
  }

  /**
   * 打开高级搜索modal
   */
  presentModal() {
    let modal = this.modalCtrl.create('StoreFilterPage');
    modal.onDidDismiss(data => {
      this.options.where_sql = data.where_sql || '';
      this.options.where_type = data.where_type || '';
      this.options.where_value = data.where_value || '';
      this.doSearch();
    })
    modal.present();
  }
}
